home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / IN_TKT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  3.5 KB  |  143 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/astaff/project/kerberos/src/lib/krb/RCS/in_tkt.c,v $
  3.  * $Author: qjb $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  */
  11.  
  12. #ifndef lint
  13. static char *rcsid_in_tkt_c =
  14. "$Id: in_tkt.c,v 4.9 89/10/25 19:03:35 qjb Exp $";
  15. #endif /* lint */
  16.  
  17. #include <conf.h>
  18. #include <mit_copy.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <krb.h>
  22. #ifdef IBMPC
  23. #include <dos.h>
  24. #include <fcntl.h>
  25. #else
  26. #include <sys/file.h> 
  27. #endif    /* IBMPC */
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #ifdef TKT_SHMEM
  31. #include <sys/param.h>
  32. #endif
  33.  
  34. extern int krb_debug;
  35.  
  36. /*
  37.  * in_tkt() is used to initialize the ticket store.  It creates the
  38.  * file to contain the tickets and writes the given user's name "pname"
  39.  * and instance "pinst" in the file.  in_tkt() returns KSUCCESS on
  40.  * success, or KFAILURE if something goes wrong.
  41.  */
  42.  
  43. in_tkt(pname,pinst)
  44.     char *pname;
  45.     char *pinst;
  46. {
  47. #ifdef IBMPC    /* use reserved memory as ticket file */
  48.     extern int lock_tkt(tkt_header far*,int);
  49.     extern void unlock_tkt(tkt_header far*);
  50.     tkt_header far *hdr;
  51.     char far *ptr;
  52.     char *value;
  53.     unsigned segment;
  54.     
  55.     if (dest_tkt()!=RET_OK)        /* Nuke old tickets */ 
  56.         return KFAILURE;
  57.     if ((hdr=tkt_ptr())==NULL) {
  58. #ifdef DEBUG
  59.         if (krb_debug)
  60.             printf("Ticket memory not reserved or"
  61.                 "corrupt environment variable\n");
  62. #endif
  63.         return KFAILURE;
  64.     }
  65.  
  66.     if (!lock_tkt(hdr,1))
  67.         return KFAILURE;
  68.     
  69.     ptr=(char far*)hdr+hdr->eof_ptr;
  70.     if (hdr->eof_ptr+strlen(pname)+strlen(pinst)+2 > hdr->buf_size) {
  71.         if (krb_debug)
  72.             printf("Ticket memory too small\n");
  73.         unlock_tkt(hdr);
  74.         return KFAILURE;        /* Reserved memory too small */
  75.     }
  76.     while (*ptr++=*pname++);    /* write user's name */
  77.     while (*ptr++=*pinst++);    /* write user instance */
  78.     hdr->eof_ptr=FP_OFF(ptr);
  79.     unlock_tkt(hdr);
  80.     return (KSUCCESS);
  81.     
  82. #else    /* !IBMPC */
  83.     int tktfile, creat();
  84.     struct stat buf;
  85.     int count;
  86.     char *file = TKT_FILE;
  87.     int fd;
  88.     register int i;
  89.     char charbuf[BUFSIZ];
  90. #ifdef TKT_SHMEM
  91.     char shmidname[MAXPATHLEN];
  92. #endif /* TKT_SHMEM */
  93.  
  94.     if (stat(file,&buf) == 0) {
  95.  
  96.         /* file already exists, and permissions appear ok, so nuke it */
  97.         if ((fd = open(file, O_RDWR, 0)) < 0)
  98.             goto out; /* can't zero it, but we can still try truncating it */
  99.  
  100.         memset(charbuf,0, sizeof(charbuf));
  101.  
  102.         for (i = 0; i < buf.st_size; i += sizeof(charbuf))
  103.             if (write(fd, charbuf, sizeof(charbuf)) != sizeof(charbuf)) {
  104.                 (void) close(fd);
  105.                 goto out;
  106.                 }
  107.     
  108.             (void) close(fd);
  109.             }         
  110.     out:
  111.  
  112.     if ((tktfile = creat(file,S_IREAD|S_IWRITE)) < 0) {
  113.         if (krb_debug)
  114.             fprintf(stderr,"Error initializing %s",TKT_FILE);
  115.         return(KFAILURE);
  116.         }
  117.     if (stat(file,&buf) < 0) {
  118.         if (krb_debug)
  119.             fprintf(stderr,"Error initializing %s",TKT_FILE);
  120.         return(KFAILURE);
  121.         }
  122.  
  123.     count = strlen(pname)+1;
  124.     if (write(tktfile,pname,count) != count) {
  125.         (void) close(tktfile);
  126.         return(KFAILURE);
  127.     }
  128.     count = strlen(pinst)+1;
  129.     if (write(tktfile,pinst,count) != count) {
  130.         (void) close(tktfile);
  131.         return(KFAILURE);
  132.     }
  133.     (void) close(tktfile);
  134. #ifdef TKT_SHMEM
  135.     (void) strcpy(shmidname, file);
  136.     (void) strcat(shmidname, ".shm");
  137.     return(krb_shm_create(shmidname));
  138. #else /* !TKT_SHMEM */
  139.     return(KSUCCESS);
  140. #endif /* TKT_SHMEM */
  141. #endif /* IBMPC */
  142. }
  143.